package eurekaClient.controller;

import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@RestController
public class RestTemplateController {

	@Autowired
	public RestTemplate restTemplate;
    
    @RequestMapping("/ribbon/hello")
    public String helloWorld() {
        return restTemplate.getForObject("http://eureka-service/hello/ribbon", String.class);
    }
    
    @HystrixCommand(fallbackMethod="error")
    @GetMapping(value = "/ribbon/add")
    public String add() throws InterruptedException {
    	if(new Random().nextInt(3) == 0){
    		return restTemplate.getForEntity("http://eureka-service/add?a=10&b=20", String.class).getBody();
    	}else{
    		String [] s = new String[5];
    		int i = 1 + 5;
    		String s1 = s[i];
    		return "超时";
    	}
    }
    
    public String error(){
    	return "request error";
    }
}
